package com.cs.common.zip;

import com.cs.common.vo.ReturnVo;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;
import java.util.ArrayList;

public class ZipWithPassword {
    public static ReturnVo zipWithPassword(ArrayList<File> fileList, String zipFileName, String password) throws ZipException {
        ReturnVo returnVo = new ReturnVo();
        ZipFile zipFile = new ZipFile(zipFileName);

        //设置压缩文件参数
        ZipParameters parameters = new ZipParameters();
        //设置压缩方法
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

        //设置压缩级别
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        //设置压缩文件加密
        parameters.setEncryptFiles(true);

        //设置加密方法
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);

        //设置aes加密强度
        parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

        //设置密码
        parameters.setPassword(password);

        //添加文件到压缩文件
        zipFile.addFiles(fileList, parameters);

        return returnVo.success();
    }
}
